<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/**
 * Init Model
 *
 */


class Location_model extends CI_Model {

	function __construct(){
		parent::__construct();
	}

	//--Get All Country Name ---//
	public function getAllCountry()
	{
		$sql = "select * from country ORDER BY (country) asc ";
		$query = $this->db->query($sql);
		$result = $query->result();
		if($result){
			return $result;
		}
		else{
			return null;
		}
	}

	//--Get All State Name ---//
	public function getAllState($countryID=0,$offset=0,$limit=1)
	{
		$sql = "select * from state where countryid = $countryID ORDER BY (statename) ASC limit $offset,$limit";
		$query = $this->db->query($sql);
		$result = $query->result();
		$query->free_result();
		if($result){
			return $result;
		}
		else{
			return null;
		}
	}

	function getStates(){
		$sql = "select * from state where status = 51";
		$result = $this->db->query($sql)->result();
		if($result) return $result;
	}
	//--Get All Country City ---//
	public function getAllCity($countryID=0, $offset=0, $limit=1,$order='RAND()')
	{
		$sql = "select * from city where countryid = $countryID ORDER BY $order limit 0,$limit";
		$query = $this->db->query($sql);
		$result = $query->result();
		$query->free_result();
		if($result){
			return $result;
		}
		else{
			return null;
		}
	}
	//--Get All Country Name-- rahul4u1994@gmail.com--//
	public function get_countries($limit=0)
	{
		$sql = "select * from country ORDER BY (country) asc limit $limit";
			
			
		$query = $this->db->query($sql);
		$result = $query->result();
		if($result){
			return $result;
		}
		else{
			return null;
		}
	}

	

	public function get_city($state_id=NULL,$num=NULL, $offset=NULL) {

		$sql = "Select * from city where stateid=$state_id and status=51";
		$query = $this->db->query($sql);
		$result = $query->result();
		if($result){
			return $result;
		}else{
			return NULL;
		}
		//var_dump($result);
		//die();
	}


	public function getPopularCity($limit=5){
		$sql = "select city, count(city) as no from post group by city having count(*)>1 order by no DESC limit $limit";
		$query = $this->db->query($sql);
		$result = $query->result();
		if($result){
			return $result;
		}else{
			return NULL;
		}
	}

	public function get_region_data($regionID=0){
		
		//$this->db->cache_on();
		$sql="select * from state where id=$regionID limit 1";
		$row = $this->db->query($sql)->row();
		//$this->db->cache_off();
		if($row){
			return $this->db->query($sql)->row();
		}else{
			return false;
		}
	}

	public function get_country_data($countryID=0){
		//$this->db->cache_on();
		$sql="select * from country where id=$countryID limit 1";
		$row = $this->db->query($sql)->row();
		//$this->db->cache_off();
		if($row){
			return $this->db->query($sql)->row();
		}else{
			return false;
		}
	}

	public function get_city_data($cityID=0){
		if($cityID){
			//$this->db->cache_on();
			$sql="select * from city where id=$cityID limit 1";
			$row = $this->db->query($sql)->row();
			//$this->db->cache_off();
			if($row){
				return $this->db->query($sql)->row();
			}else{
				return false;
			}
			
		}
	}
	
	//--Get Cities By State-- rahul4u1994@gmail.com--//
	function get_cities_by_state($stateID = null){
		//echo $stateID;die;
		$this->db->select('*');
	
		if($stateID != NULL){
			$this->db->where('stateid', $stateID);
		}
		$query = $this->db->get('city');

		$cities = array();
		
		if($query->result()){
			foreach ($query->result() as $city) {
				$cities[$city->id] = $city->cityname;
			}
			
			
			return $cities;
	
		}else{
			return FALSE;
		}
	
	
	}

}
/* End of file*/
